<?php
    require_once ("../accesobd.php");
    
    $id = $_POST["ID"];
    $nombre = $_POST["nombre"];
    
    
        function begin(){
        pg_query("BEGIN;");
    }
    
    function commit(){
        pg_query("COMMIT;");
    }
    
    function rollback(){
        pg_query("ROLLBACK;");
    }

    if (!$id || !$nombre){ 
        echo '<script type="text/javascript">', 'window.alert("Error: Campo vacio");',
                        'location.reload(true);'
                    , '</script>'
                 ;
    }else{
        $buscardpto = "select d.iddpto from departamento as d where d.iddpto='$id';";
        $consultar = pg_query($conex,$buscardpto);
        $iddptoc = pg_fetch_array($consultar);
        
        if(strcmp($iddptoc['iddpto'],"")==0){
           echo '<script type="text/javascript">', 'window.alert("Error: Departamento no existe");',
                        'location.reload(true);'
                    , '</script>'
                 ;
                 pg_close($conex);
                return 0;
        }
        else{
            $query = pg_query($conex,"UPDATE departamento
                                SET  nombdpto='$nombre'
                                WHERE iddpto='$id';
                          ");
            echo '<script type="text/javascript">', 'window.alert("Campo agregado con éxito");',
                    'location.reload(true);'
                , '</script>'
             ;
        }    
        pg_close($conex);
     }
    
?>

